/*
*	Copyright (C) 2014  Julio Montes, Mario Carrillo
*
*	Julio Montes e-mail: imc.coder@gmail.com
*
*	Mario Carrillo e-mail: mario_90ago@live.com.mx
*
*	BSD 3-Clause License
*/

#include "include/iaFindDialog.h"

#include "include/iaManager.h"
#include "sdk/include/editormgr.h"
#include "sdk/include/editor.h"

BEGIN_EVENT_TABLE( iaFindDialog, iaFindDialogBase )
    EVT_BUTTON( wxID_FIND, iaFindDialog::OnFind )
    EVT_TEXT_ENTER( iaID_FDBASE_CBFIND, iaFindDialog::OnFind )
    EVT_SHOW( iaFindDialog::OnShow )
END_EVENT_TABLE()

iaFindDialog::iaFindDialog( wxWindow *parent )
    : iaFindDialogBase( parent, wxID_ANY, wxT( "Find" ), wxDefaultPosition, wxSize( 400, 400 ) )
{
    wxBoxSizer *bsButtons = new wxBoxSizer( wxHORIZONTAL );

    wxButton *btnFindReplace = new wxButton( this, wxID_FIND );
    wxButton *btnCancel = new wxButton( this, wxID_CANCEL );
    bsButtons->Add( btnFindReplace, 0, wxRIGHT, 5 );
    bsButtons->Add( btnCancel, 0, wxLEFT, 5 );

    bsMain->Add( bsButtons, 0, wxALIGN_CENTRE | wxTOP | wxBOTTOM, 20 );
}

iaFindDialog::~iaFindDialog()
{

}


void iaFindDialog::OnFind( wxCommandEvent &evt )
{
    if( cbFind->GetValue() != wxEmptyString )
    {
        EndModal( wxID_FIND );
        lastWord = cbFind->GetValue();
        if( cbFind->FindString( lastWord, true ) == wxNOT_FOUND )
            cbFind->Append( lastWord );

        Find( rbUp->GetValue() );
    }
}



void iaFindDialog::Find( bool dirUp )
{

    if( lastWord == wxEmptyString )
    {
        ShowModal();
        return;
    }

    sdk::Editor *se = sdk::EditorManager::Get()->GetCurrentEditor();

    flags = 0;
    flags |= cbMatchCase->IsChecked() ? wxSCI_FIND_MATCHCASE : 0;
    flags |= cbRegExp->IsChecked() ? wxSCI_FIND_REGEXP : 0;
    flags |= cbPosixRegExp->IsChecked() ? wxSCI_FIND_POSIX : 0;
    flags |= cbWholeWord->IsChecked() ? wxSCI_FIND_WHOLEWORD : 0;
    flags |= cbStartWord->IsChecked() ? wxSCI_FIND_WORDSTART : 0;

    const long currentPos( se->GetCurrentPos() );
    const long startWord( se->WordStartPosition( currentPos, true ) );
    const long endWord( se->WordEndPosition( currentPos, true ) );
    const long lenText( se->GetLength() );

    const bool directionUp( !cbRegExp->IsChecked() && dirUp );

    long startFind;
    long endFind;
    long restartFind;

    if( rbStartFile->GetValue() )
    {
        startFind = directionUp ? lenText : 0;
        endFind = directionUp ? 0 : lenText;
        restartFind = directionUp ? startFind : 0;
    }
    else
    {
        if( rbGlobal->GetValue() )
        {
            //startFind = directionUp ? startWord : ( endWord > currentPos ? startWord : endWord );
            startFind = directionUp ? startWord : endWord;
            endFind = directionUp ? 0 : lenText;
            restartFind = directionUp ? lenText : 0;
        }
        else
        {
            startFind = directionUp ? se->GetSelectionEnd() : se->GetSelectionStart();
            endFind = directionUp ? se->GetSelectionStart() : se->GetSelectionEnd();
            restartFind = startFind;
        }
    }

    int lenFound;
    long posFound = se->FindText( startFind, endFind, lastWord, flags, &lenFound );

    fprintf( stderr, "1 - start: %ld, end: %ld\n", startFind, endFind );

    if( posFound != wxSCI_INVALID_POSITION )
    {
        se->EnsureVisible( se->LineFromPosition( posFound ) );
        se->SetSelection( posFound, posFound + lenFound );
        se->EnsureCaretVisible();
    }
    else
    {
        if( cbAutoWrap->IsChecked() )
        {
            posFound = se->FindText( restartFind, startFind, lastWord, flags, &lenFound );
            fprintf( stderr, "2 - start: %ld, end:%ld\n", restartFind, startFind );

            if( posFound != wxSCI_INVALID_POSITION )
            {
                se->EnsureVisible( se->LineFromPosition( posFound ) );
                se->SetSelection( posFound, posFound + lenFound );
                se->EnsureCaretVisible();
                return;
            }
        }
        wxMessageBox( wxT( "Not found: " ) + lastWord, wxT( "Find" ) );
    }
}




void iaFindDialog::OnShow( wxShowEvent& evt )
{
    sdk::Editor *se = sdk::EditorManager::Get()->GetCurrentEditor();

    if( evt.IsShown() )
    {
        const wxString &selText( se->GetSelectedText() );
        if( selText.Find( wxT( '\n' ) ) != wxNOT_FOUND )
        {
            cbFind->SetValue( wxT( "" ) );
            rbSelectText->SetValue( true );
        }
        else
        {
            cbFind->SetValue( selText );
            rbGlobal->SetValue( true );
        }

        cbFind->SetFocus();
    }

    evt.Skip();
}


//iaFindPanel::iaFindPanel( wxWindow *parent )
//    : iaFindPanelBase( parent )
//{
//    wxBoxSizer *bsButtons = new wxBoxSizer( wxHORIZONTAL );
//
//    wxButton *btnFindReplace = new wxButton( this, wxID_FIND );
//    wxButton *btnCancel = new wxButton( this, wxID_CANCEL );
//    bsButtons->Add( btnFindReplace, 0, wxRIGHT, 5 );
//    bsButtons->Add( btnCancel, 0, wxLEFT, 5 );
//
//    bsMain->Add( bsButtons, 0, wxALIGN_CENTRE | wxTOP | wxBOTTOM, 20 );
//
//    GetGrandParent()->Connect( wxEVT_SHOW, wxShowEventHandler( iaFindPanel::OnShow ), NULL, this );
//}
//
//iaFindPanel::~iaFindPanel()
//{
//    //dtor
//}
//
//void iaFindPanel::OnRegExp( wxCommandEvent &evt )
//{
//    bsMain->Show( sbsDirection, !evt.IsChecked(), true );
//}
//
//void iaFindPanel::OnFind( wxCommandEvent &evt )
//{
//    if( cbFind->GetValue() != wxEmptyString )
//    {
//        iaManager::Get()->GetDialogManager()->GetFindDialog()->EndModal( wxID_FIND );
//        iaEditor *se = iaManager::Get()->GetEditorManager()->GetCurrentEditor();
//
//        lastWord = cbFind->GetValue();
//
//        if( cbFind->FindString( lastWord, true ) == wxNOT_FOUND )
//            cbFind->Append( lastWord );
//
//        flags = 0;
//        flags |= cbMatchCase->IsChecked() ? wxSCI_FIND_MATCHCASE : 0;
//        flags |= cbRegExp->IsChecked() ? wxSCI_FIND_REGEXP : 0;
//        flags |= cbPosixRegExp->IsChecked() ? wxSCI_FIND_POSIX : 0;
//        flags |= cbWholeWord->IsChecked() ? wxSCI_FIND_WHOLEWORD : 0;
//        flags |= cbStartWord->IsChecked() ? wxSCI_FIND_WORDSTART : 0;
//
//        long start_find, end_find, re_start_find;
//        AuxFind( &start_find, &end_find, &re_start_find );
//
//        int lenFound;
//        long pos = se->FindText( start_find, end_find, lastWord, flags, &lenFound );
//
//        if( pos != wxSCI_INVALID_POSITION )
//        {
//            se->EnsureVisible( se->LineFromPosition( pos ) );
//            se->SetSelection( pos, pos + lenFound );
//            se->EnsureCaretVisible();
//        }
//        else
//        {
//            if( cbAutoWrap->IsChecked() )
//            {
//                pos = se->FindText( re_start_find, start_find, lastWord, flags, &lenFound );
//
//                if( pos != wxSCI_INVALID_POSITION )
//                {
//                    se->EnsureVisible( se->LineFromPosition( pos ) );
//                    se->SetSelection( pos, pos + lenFound );
//                    se->EnsureCaretVisible();
//                    return;
//                }
//            }
//
//            wxMessageBox( wxT( "Not found: " ) + lastWord, wxT( "Find" ) );
//        }
//    }
//}

//
//void iaFindPanel::FindNext()
//{
//    if( lastWord != wxEmptyString )
//    {
//        iaEditor *se = iaManager::Get()->GetEditorManager()->GetCurrentEditor();
//
//        long start_find( se->WordEndPosition( se->GetCurrentPos(), false ) );
//        long end_find( se->GetLength() );
//        long re_start_find( 0 );
//
//        int lenFound;
//        long pos = se->FindText( start_find, end_find, lastWord, flags, &lenFound );
//
//        if( pos != wxSCI_INVALID_POSITION )
//        {
//            se->EnsureVisible( se->LineFromPosition( pos ) );
//            se->SetSelection( pos, pos + lenFound );
//            se->EnsureCaretVisible();
//        }
//        else
//        {
//            if( cbAutoWrap->IsChecked() )
//            {
//                pos = se->FindText( re_start_find, start_find, lastWord, flags, &lenFound );
//
//                if( pos != wxSCI_INVALID_POSITION )
//                {
//                    se->EnsureVisible( se->LineFromPosition( pos ) );
//                    se->SetSelection( pos, pos + lenFound );
//                    se->EnsureCaretVisible();
//                    return;
//                }
//            }
//
//            wxMessageBox( wxT( "Not found: " ) + lastWord, wxT( "Find" ) );
//        }
//    }
//    else
//        iaManager::Get()->GetDialogManager()->GetFindDialog()->ShowModal();
//}
//
//
//
//void iaFindPanel::FindPrevious()
//{
//    if( lastWord != wxEmptyString )
//    {
//        iaEditor *se = iaManager::Get()->GetEditorManager()->GetCurrentEditor();
//
//        long start_find( se->WordStartPosition( se->GetCurrentPos(), false ) );
//        long end_find( se->GetLength() );
//        long re_start_find( 0 );
//
//        int lenFound;
//        long pos = se->FindText( start_find, re_start_find, lastWord, flags, &lenFound );
//
//        if( pos != wxSCI_INVALID_POSITION )
//        {
//            se->EnsureVisible( se->LineFromPosition( pos ) );
//            se->SetSelection( pos, pos + lenFound );
//            se->EnsureCaretVisible();
//        }
//        else
//        {
//            if( cbAutoWrap->IsChecked() )
//            {
//                pos = se->FindText( end_find, start_find, lastWord, flags, &lenFound );
//
//                if( pos != wxSCI_INVALID_POSITION )
//                {
//                    se->EnsureVisible( se->LineFromPosition( pos ) );
//                    se->SetSelection( pos, pos + lenFound );
//                    se->EnsureCaretVisible();
//                    return;
//                }
//            }
//
//            wxMessageBox( wxT( "Not found: " ) + lastWord, wxT( "Find" ) );
//        }
//    }
//    else
//        iaManager::Get()->GetDialogManager()->GetFindDialog()->ShowModal();
//}
//
//
//void iaFindPanel::AuxFind( long *start_find, long *end_find, long *re_start_find )
//{
//    iaEditor *se = iaManager::Get()->GetEditorManager()->GetCurrentEditor();
//    long current= se->WordEndPosition( se->GetCurrentPos(), false );
//    long start = ( rbGlobal->GetValue() ? 0 : se->GetSelectionStart() );
//    long end = ( rbGlobal->GetValue() ? se->GetLength() : se->GetSelectionEnd() );
//
//    if( !cbRegExp->IsChecked() && rbUp->GetValue() )
//    {
//        long tmp = start;
//        start = end;
//        end = tmp;
//        //current = se->GetSelectedText().CmpNoCase( lastWord ) ? current : current - lastWord.Len();
//        current = se->WordStartPosition( se->GetCurrentPos(), false );
//    }
//
//    *start_find = ( rbStartFile->GetValue() ? start : current );
//    *end_find = end;
//    *re_start_find = start;
//}
